package com.flybook.utils;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;

@Component
@Configuration
public class FlyBookService {

    @Value("${feiShuParam.appId}")
    private String appId;

    // 令牌秘钥
    @Value("${feiShuParam.appSecret}")
    private String appSecret;

    public String GenSign(long timestamp) throws NoSuchAlgorithmException, InvalidKeyException {

        //把timestamp+"\n"+密钥当做签名字符串
        String stringToSign = timestamp + "\n" + appSecret;
        //使用HmacSHA256算法计算签名
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(new byte[]{});

        return new String(Base64.encodeBase64(signData));
    }
}
